package com.cn.util;

import javax.swing.*;
import java.sql.*;

public class JDBCUtils {
    // 加载驱动并建立数据库连接
    //驱动类的类名
    private static final String DRIVERNAME="com.mysql.cj.jdbc.Driver";
    //连接数据的URL路径
    private static final String URL="jdbc:mysql://localhost:3306/db_goods?serverTimezone=UTC&characterEncoding=UTF-8";
    //数据库登录账号
    private static final String USER="root";
    //数据库登录密码
    private static final String PASSWORD="123456";
    //加载驱动
    static{
        try {
            Class.forName(DRIVERNAME);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //获取数据库连接
    public static Connection getConnection() {
        try {
            return DriverManager.getConnection(URL,USER,PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    //查询
    public static ResultSet query(String sql) {
        System.out.println(sql);
        //获取连接
        Connection connection=getConnection();
        PreparedStatement psd;
        try {
            psd = connection.prepareStatement(sql);
            return psd.executeQuery();
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null,"执行语句出错\n"+e.toString());
            e.printStackTrace();
        }
        return null;
    }
    //增、删、改、查
    public static int updataInfo(String sql) {
        System.out.println(sql);
        //获取连接
        Connection connection=getConnection();
        try {
            PreparedStatement psd=connection.prepareStatement(sql);
            return psd.executeUpdate();
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null,"执行语句出错\n"+e.toString());
            e.printStackTrace();
        }
        return 0;
    }
    //关闭连接
    public  static  void colse(ResultSet rs,Statement stmt,Connection  conn) throws Exception{
        try { if (rs != null){ rs.close(); }
            if (stmt != null) { stmt.cancel(); }
            if (conn != null) { conn.close(); }
        } catch (Exception e) {
            e.printStackTrace(); throw new Exception();
        }
    }

    // 关闭数据库连接释放资源
    public static void release(Connection connection,Statement statement, ResultSet resultSet) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}


